package dashboard;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ActivityController {
	Activity currentActivity;

	public ActivityController()
	{

	}

	/** Adds an activity to a User object if all the input is correct and not already timing.
	 * @param courseName
	 * @param currentUser
	 * @param type
	 * @return null if no error, List with error(s) if error(s)
	 */
	public List<String> addActivity(String courseName, User currentUser, String type) 
	{
		List<String> errors = new ArrayList<String>();

		if(currentUser==null) errors.add("Not logged in.");
		if(currentUser.getRole()==User.Role.TUTOR) errors.add("Tutors can not track their activities.");
		if(courseName.equals("") || courseName.isEmpty() || courseName==null) errors.add("No valid courseName");
		User user = UserRegistry.getInstance().getUser(currentUser.getUserName());
		if(errors.size()!=0) return errors;

		long courseId = CourseRegistry.getInstance().getCourseId(courseName);
		if(courseId==-1) errors.add("Not a valid course");
		if(type.isEmpty()) errors.add("No type given.");			
		if(user.getTimerStatus())errors.add("You're already timing");

		if(errors.size()!=0) return errors;
		
		Date now = new Date();
		currentActivity = 	new Activity(now, courseId, type);	
		user.addActivity(currentActivity);
		user.setTiming(true);
		UserRegistry.getInstance().addUser(user);
		
		return errors;
	}

	/**
	 * Ends an activity by updating the ongoing activity of the user if input is correct and already timing.
	 * @param apprec
	 * @param nrOfPages
	 * @param location
	 * @param currentUser
	 * @param seconds 
	 * @param minutes 
	 * @param hours 
	 * @return true if success
	 */
	public List<String> endActivity(int apprec, String nrOfPages, String location, User currentUser, int hours, int minutes, int seconds, Date stop){

		List<String> errors = new ArrayList<String>();

		if(currentUser==null) errors.add("Not logged in.");
		if(errors.size()!=0) return errors;
		User user = UserRegistry.getInstance().getUserById(currentUser.getId());

		if(user==null) errors.add("You're not a valid user");
		if(errors.size()!=0) return errors;
		
		if(!user.getTimerStatus())errors.add("You're status says you're not timing an activity.");
		if(location==null || location.isEmpty()) errors.add("Not a valid location.");
		if(hours==0 && minutes==0 && seconds==0) errors.add("No valid time");
		
		if(errors.size()!=0) return errors;
		
		if(location.length()>100) errors.add("Location is limited two 100 chars");
		if(apprec<1 || apprec>10) errors.add("No valid appreciation");
		if(hours==-1 || minutes==-1 || seconds==-1) errors.add("The time inserted isn't valid.");
		if(stop==null) errors.add("Please try again and check if sessions are enabled");

		if(errors.size()!=0) return errors;

		if(user.getActivities()==null) errors.add("something went wrong with your activities.");
		if(errors.size()!=0) return errors;
		if(user.getActivities().size()==0) errors.add("You're not timing an activity.");
		if(errors.size()!=0) return errors;

		List<Activity> activities= user.getActivities();
		currentActivity = activities.get(activities.size()-1);
		if(currentActivity==null) errors.add("The system doesn't find your current activity");
		if(errors.size()!=0) return errors;
		int nr=0;
		if(currentActivity.getType().equals("Studying")){
			if(nrOfPages==null){
				errors.add("not a valid number of pages");
			}
		if(errors.size()!=0) return errors;
		
	
		int i =1;
		while(i<=1000){
			if(nrOfPages.equals(""+i)){
				nr =i;
				i=1001;
			}
			i++;
		}

		if(i==1001)	errors.add("Total number of pages must vary between 1 and 1000 pages.");
		}
		
		
		long time =hours*3600*1000+minutes*60*1000+seconds*1000;
		Date start = currentActivity.getStart();
		
		if(stop.getTime() - start.getTime() < time) errors.add("You can only decrease the measured time.");
		if(errors.size()!=0) return errors;
	
		if(start.after(stop)) errors.add("Er loopt iets fout met de datums");
		if(errors.size()!=0) return errors;
		
	/*	// 6 Sigma methode
	 
		long mean= getMean();
		double sigma = getDeviation(mean);
		if(time> mean +6*sigma || time< mean - 6*sigma) errors.add("It seems like this Activity took too long/short.");
		if(errors.size()!=0) return errors;*/
		
		 
		
		time=time/1000;
		currentActivity.setAppreciation(apprec);
		currentActivity.setNrOfPages(nr);
		currentActivity.setPlace(location);
		currentActivity.setDuration(time);

		user.setActivity(currentActivity);
		user.setTiming(false);
		UserRegistry.getInstance().addUser(user);
		return errors;
	}

	public long getMean(){
		List<User> users= UserRegistry.getInstance().getAllUsers();
		long total=0;
		long i=0;
		for(User user: users){
			
			List<Activity> activities = user.getActivities();
			for(Activity activity: activities){
				total+=activity.getDuration();
				i++;
			}
		}
		long mean= total/i;		
		return mean;
	}
	
	public double getDeviation(long mean){
		List<User> users= UserRegistry.getInstance().getAllUsers();
		long total=0;
		long i=0;
		for(User user: users){
			
			List<Activity> activities = user.getActivities();
			for(Activity activity: activities){
				total+=(java.lang.Math.pow(activity.getDuration(),2)-java.lang.Math.pow(mean,2));
				i++;
			}
		}
		double sigma= Math.sqrt(total/i);		
		return sigma;
	}
}